/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.abc;


import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.sapra.core.DBManager;
import org.sapra.dao.CCosto;
import org.sapra.dao.Empresa;
import org.sapra.logon.dLogin;

/**
 *
 * @author ppratsc
 */
public class dCentroCosto {
    public static List<CCosto> getCCosto(Empresa emp){
        List<CCosto> resp = null;
        String sql = "SELECT * FROM centrocosto WHERE fk_empresaid = ";
        sql += emp.getEmpresaId();
        sql += " ORDER BY nombre";
        
        try {
            resp = DBManager.getQuery(CCosto.class, sql);
        } catch (Exception ex) {
            Logger.getLogger(dCentroCosto.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return resp;
    }

    public static int SaveCCosto(CCosto ccos){
        int resp=0;
        String nueva=ccos.getNombre().trim()+" "+ccos.getDescripcion().trim();
        if(nueva.length()>95){
            nueva=nueva.substring(0,95);
        }
        String sql = "UPDATE centrocosto SET ";
        sql += "nombre = '" + nueva.toUpperCase().trim() + "'";
        sql += ",descripcion=''";
        sql += ",fk_empresaid="+ ccos.getFk_empresaId();
        sql += " WHERE centrocostoid = " + ccos.getCentroCostoId();
        try {
            resp = DBManager.Update(sql);
        } catch (Exception ex) {
            Logger.getLogger(dCentroCosto.class.getName()).log(Level.SEVERE, null, ex);
        }

        return resp;
    }

    static int Delete(CCosto ccos) {
        int resp=0;
        String sql = "DELETE FROM centrocosto WHERE centrocostoid = " + ccos.getCentroCostoId();
        try {
            resp = DBManager.Update(sql);
        } catch (Exception ex) {
            Logger.getLogger(dCentroCosto.class.getName()).log(Level.SEVERE, null, ex);
        }
        return resp;
    }

    static int Nuevo(CCosto ccos) {
        int resp =0;

        String nueva=ccos.getNombre().trim();
        String nuevo=ccos.getDescripcion().trim();
        if(nueva.length()>95){
            nueva=nueva.substring(0,95);
        }


        String sql = "INSERT INTO centrocosto(nombre,descripcion,fk_empresaid) VALUES(";
        sql += "'" + nueva.toUpperCase().trim() + "'";
        sql += ",'"+nuevo.toUpperCase().trim() + "'";
        sql += "," + dLogin.getEmpresa().getEmpresaId() + ")";
        
         try {
            resp = DBManager.Update(sql);
        } catch (Exception ex) {
            Logger.getLogger(dCentroCosto.class.getName()).log(Level.SEVERE, null, ex);
        }

        return resp;
        
    }

    public static CCosto getCCosto(int opc){
        CCosto resp = null;

        String sql = "SELECT ";
        sql += "nombre ";
        sql += " FROM ";
        sql += "centrocosto";
        sql += " WHERE ";
        sql += " centroCostoId = " + opc;

        List<CCosto> ccs;
        try {
            DBManager db=new DBManager();
            // Busca la universidad en la base de datos
            ccs = db.getQuery(CCosto.class,sql);
            db=null;
            resp=new CCosto();
            resp=ccs.get(0);
        } catch (Exception ex) {
            Logger.getLogger(dCentroCosto.class.getName()).log(Level.SEVERE, null, ex);
            return resp;
        }

        return resp;
    }



}
